package com.tencent.qcloud.tim.uikit.component.dialog

import android.content.Context
import android.text.Editable
import android.text.Selection
import android.text.TextWatcher
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.impl.FullScreenPopupView
import com.lxj.xpopup.interfaces.OnSelectListener
import com.manor.common.utils.str
import com.tencent.qcloud.tim.uikit.R
import kotlinx.android.synthetic.main.dialog_modify_name.view.*

/**
 * 修改信息


 * @date  2020/7/31  11:07
 * @version 1.0
 */
public class ModifyNameDialog(context: Context, private val title: String, private val subTitle: String, private val
hintStr: String,val contentStr: String,private val maxCount: Int, private val listener: OnSelectListener)
    : FullScreenPopupView(context), TextWatcher {
    private var curWords = 0

    companion object {
        fun load(context: Context, title: String, subTitle: String,hintStr: String,contentStr: String,
                 maxCount: Int, listener: OnSelectListener) {
            XPopup.Builder(context)
                    .hasStatusBar(true)
                    .asCustom(ModifyNameDialog(context, title, subTitle, hintStr,contentStr, maxCount, listener))
                    .show()
        }
    }

    override fun getImplLayoutId(): Int = R.layout.dialog_modify_name

    override fun onCreate() {
        super.onCreate()
        ivDismiss.setOnClickListener { dismiss() }
        tvConfirm.setOnClickListener { doSubmit() }
        tvTitle.text = title
        tvSubTitle.text = subTitle
        if (contentStr.isNotEmpty()){
            edText.setText(contentStr)
        }else{
            edText.hint = hintStr
        }

        tvNum.text = "${edText.length()}/$maxCount"
        edText.addTextChangedListener(this)
    }

    private fun doSubmit() {
        if (edText.str().trim().isEmpty())
            return
        listener.onSelect(0, edText.str())
        dismiss()
    }

    override fun afterTextChanged(s: Editable?) {
        // //已输入字数
        //                enteredWords= maxCount - s.length();
        //                //TextView显示剩余字数
        //                textViewRemainingWords.setText(100 - enteredWords+ "/100字");
        //                selectionStart = editText.getSelectionStart();
        //                selectionEnd = editText.getSelectionEnd();
        //                if (enterWords.length() > maxCount) {
        //                    //删除多余输入的字（不会显示出来）
        //                    s.delete(selectionStart - 1, selectionEnd);
        //                    int tempSelection = selectionEnd;
        //                    editText.setText(s);
        //                    //设置光标在最后
        //                    editText.setSelection(tempSelection);
        //                }

        curWords = maxCount - (s?.length ?: 0)
        tvNum.text = "$curWords/$maxCount"

        if (s?.length ?: 0 > 0) {
            if (!tvConfirm.isSelected) {
                tvConfirm.isSelected = true
            }
        } else {
            if (tvConfirm.isSelected) {
                tvConfirm.isSelected = false
            }
        }


        if (s?.length ?: 0 > maxCount) {
            edText.setText(s?.subSequence(0, maxCount));
            Selection.setSelection(edText.text, maxCount);
        }
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
    }


}